//
//  PurchaseAllListViewController.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/1.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "PurchaseAllListViewController.h"
#import "PurchaseAllListViewModel.h"
#import "PurchaseInformationViewController.h"
#import "PurchaseAllListModel.h"
#import "OrderAllListViewController.h"
#import "PaymentDetailsViewController.h"

@interface PurchaseAllListViewController ()<PurchaseListDelegate>
{
    NSMutableDictionary * requestDic;
}
@property (nonatomic, strong) PurchaseAllListViewModel *purListViewModel;
@property (nonatomic, strong) PurchaseAllListModel *model;


@end

@implementation PurchaseAllListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.purListViewModel = [[PurchaseAllListViewModel alloc] initWithViewToViewModel:self.groupedTableView theSuperView:self.view];
    self.purListViewModel.type = self.type;
    self.purListViewModel.delegate = self;
    kWeakSelf(self);
    self.purListViewModel.BlockBottomBtn = ^(GLGWButton * _Nonnull btn, PurchaseAllListDataInfosModel * _Nonnull inofModel) {
        [weakself bottomBtnClikWith:btn model:inofModel];
    };
    NSString *str = [NSString stringWithFormat:@"%ld",(long)self.type];
//    requestDic = [NSMutableDictionary dictionaryWithDictionary:@{@"status":str ,@"userId":koString(self.userId), @"pageSize":@10}];
    requestDic = [NSMutableDictionary dictionaryWithDictionary:@{@"status":@"0",@"userId":@"123456789",@"pageSize":@10}];
    [self headerRereshing];
}

/*
 0: 刷新求购单
 1: 删除
 2: 查看退款
 3: 查看订单
 */
- (void)bottomBtnClikWith:(GLGWButton *)btn model:(PurchaseAllListDataInfosModel *)model
{
    switch (btn.tag) {
        case 320:// 刷新求购单
            [self headerRereshing];
            break;
        case 321: // 删除
            break;
        case 322:{ // 查看退款
            PaymentDetailsViewController *vc = [PaymentDetailsViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 323:{ // 查看订单
            OrderAllListViewController *vc = [OrderAllListViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
}

#pragma mark -- PurchaseListDelegate
- (void)purchaseDidSelectRowAtModel:(PurchaseAllListDataInfosModel *)model
{
    PurchaseInformationViewController *purVC = [PurchaseInformationViewController new];
    purVC.orderStr = model.ID;
    [self.navigationController pushViewController:purVC animated:YES];
}

#pragma mark -- 数据请求
- (void)headerRereshing
{
    self.page = 1;
    [self loadData];
}

- (void)footerRereshing
{
    if (self.page < self.model.data.total) {
        self.page ++;
        [self loadData];
    } else {
        [GLGWProgressHUD showNotice:@"没有更多服务的买家了！" view:nil];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }
}

- (void)loadData
{
    [requestDic setObject:@(self.page) forKey:@"page"];
    @weakify(self)
    [[self.purListViewModel.service_RecommendCommand execute:requestDic] subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        self.model = (PurchaseAllListModel *)x;
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    } error:^(NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }];
}

@end
